<?php
/**
* Climbing up to the dir tree to the root from current dir
* 
* @param mixed $folder_name - the folder we seeking for, only folder name without any slash
* @return false if we could not find dir, or path to this dir without trailing slash
*/
function SeekFolderAtParents($folder_name){
    $cur_dir = getcwd();
    
    $cur_dir=trim($cur_dir); // current dir for testing
    $old_dir= ""; // to determinate where to stop script
    
    // Climbing to the dir root, template for cutting of tail
    // have to add hack in REXEX cuz [/////] dos not work
    $cut_off_pattern="/\\\\{0,1}\/{0,1}(\w*)$/i";
    
    // Here will be stored paretns of current dir
    $childs_array=array();
    
   // Breking with loop at the and of block
    for( ;  ; ){
        echo "<b>cur_dir:</b>!!! $cur_dir <br>";
        $cur_dir=preg_replace($cut_off_pattern,"",$cur_dir);
        
        if(is_dir($cur_dir)){
            $childs_array=scandir($cur_dir);
            if(in_array($folder_name,$childs_array)){
                return $cur_dir."/".$folder_name;
            }
        }
        
        // We start to repeating the same, need to break
        if($old_dir==$cur_dir || (false==is_dir($cur_dir))) break;
        $old_dir=$cur_dir;
    }
    return false;
}

$ans=SeekFolderAtParents("debug_fast");
var_dump($ans);
var_dump(is_dir($ans));
?>
